<?php

$merchantCode="LIIUIR81V54XZYIZB91D";  // Don't put this in a public readable place
$password="f9c3bwUjjzIybJzIfDMyb9kqCj2X7JqQv";  // Don't put this in a public readable place
$orderCode="exampleoo".time();

// Shopper specific details
$shopperEmailAddress="zhizaipiao@126.com";
$firstName="Job";
$lastName="Yang";
$shopperStreet="Massachusetts Avenue Room 3-108";
$postalCode="02139";
$shopperCity="Cambridge";
$shopperTelephone="0016172533400";
$shopperState="MA";
$countryCode="US";

$url ="https://$merchantCode:$password@secure-test.worldpay.com/jsp/merchant/xml/paymentService.jsp";//it is better to keep this url outside your HTML dir which has public (internet) access

//$xml is the order string to send to bibit
echo $xml='<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//WorldPay/DTD WorldPay PaymentService v1//EN" 
"http://dtd.worldpay.com/paymentService_v1.dtd">
 <paymentService version="1.4" merchantCode="OWNONLYUSD">
 <submit>
 <order orderCode="'.$orderCode.'" installationId="1033861">
 <description>20 English roses from OWNONLYUSD Webshop</description>
 <amount value="2600" currencyCode="GBP" exponent="2"/>
 <orderContent>
 <! [CDATA [PlaceHTML content here] ] >
 </orderContent>
 <paymentMethodMask>
 <include code="ALL"/>
 </paymentMethodMask>
 <shopper>
 <shopperEmailAddress>'.$shopperEmailAddress.'</shopperEmailAddress>
 </shopper>
 <shippingAddress>
 <address>
 <firstName>'.$firstName.'</firstName>
 <lastName>'.$lastName.'</lastName>
 <address1>'.$shopperStreet.'</address1>
 <postalCode>'.$postalCode.'</postalCode>
<city>'.$shopperCity.'</city>
<state>'.$shopperState.'</state>
<countryCode>'.$countryCode.'</countryCode>
<telephoneNumber>'.$shopperTelephone.'</telephoneNumber>
 </address>
 </shippingAddress>
 <billingAddress>
 <address>
 <firstName>'.$firstName.'</firstName>
 <lastName>'.$lastName.'</lastName>
 <address1>'.$shopperStreet.'</address1>
 <postalCode>'.$postalCode.'</postalCode>
<city>'.$shopperCity.'</city>
<state>'.$shopperState.'</state>
<countryCode>'.$countryCode.'</countryCode>
<telephoneNumber>'.$shopperTelephone.'</telephoneNumber>
 </address>
 </billingAddress>
 </order>
 </submit>
 </paymentService>';
//$xml is the order string to send to bibit

//let's make the socket connection

//make socket connection with bibit
//the curl library is used. make sure you have it installed propperly
// more info: http://www.php.net/manual/en/ref.curl.php
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //$xml is the xml string
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOPROGRESS, 0);

// echo "ch: $ch<HR>" ;
echo "<br />";
$result = curl_exec ($ch); // result will contain XML reply from WorldPay curl_close ($ch);

// 检查是否有错误发生
if(curl_errno($ch))
{
    echo 'Curl error: ' . curl_error($ch);
}
echo "<br />";
// 关闭句柄
curl_close($ch);


if ( $result == false )
{
        echo $error = "Curl could not retrieve page '$url', curl_exec returns false";
        exit;
}

//now we have the result from WorldPay containing the xml answer. we need to parse this through the XML parser
// initialize parser //using SAX parser
//
//more info on SAX/PHP on these urls
// http://www.devshed.com/Server_Side/XML/XMLwithPHP/XMLwithPHP1/page1.html
// http://www.php.net/manual/en/ref.xml.php


function startElement($parser, $name, $attrs) 
{
	global $currentTag,$ordercode,$referenceID,$errorcode,$url_togoto;
	$currentTag = $name;

	switch ($name) {
		case "ERROR": 

/*	
THERE IS AN XML ERROR REPLY
1 : internal error, could be everything
2 : parse error, invalid xml
3 : invalid number of transactions in batch
4 : security error
5 : invalid request
6 : invalid content, occurs when xml is valid but content of xml not
7 : payment details in the order element are incorrect
*/
		$errorcode = $attrs['CODE']; //example of how to catch the error code number (i.e. 1 to 7)
		$url_error="error_order.php";
			break;

		case "REFERENCE":
			$referenceID = $attrs['ID'];//for storage in your own database
			break;
			
		case "ORDERSTATUS":
			$ordercode = $attrs['ORDERCODE'];
			break;
			
		default:
			break;
	}
}


//////////////////
function endElement($parser, $name) {
	global $currentTag;
	$currentTag = "";
}

/////////////////
function characterData($parser, $result) {

	global $currentTag;
	global $url_togoto;

	switch ($currentTag) {

	case "REFERENCE":
		//there is a REFERENCE so there must be an url which was provided by WorldPay for the actual payment. echo $result;
		$url_togoto=$result;
		break;

	default:
		break;
	}
}

global $currentTag,$ordercode,$referenceID,$errorcode,$url_togoto;//define globals
$xml_parser = xml_parser_create();

// set callback functions
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

if (!xml_parse($xml_parser, $result))
{
    die(sprintf("XML error: %s at line %d",
	xml_error_string(xml_get_error_code($xml_parser)),
	xml_get_current_line_number($xml_parser)));
}

// clean up
xml_parser_free($xml_parser);
//now we have a few important variables. Depending on what variables are set you can now perform an action like storing id's in your database or refer to a different url.
echo "ordercode=$ordercode<BR>";
echo "referenceID=$referenceID<BR>";
echo "errorcode=$errorcode<BR>";
echo "url_togoto=$url_togoto<BR>";





?> 